Re: [GENERAL] How to do this in SQL? - Mailing list pgsql-general

From Stuart Rison
Subject Re: [GENERAL] How to do this in SQL?
Date
Msg-id v04003a07b326a26d4f2e@[128.40.242.190]
Whole thread Raw
In response to How to do this in SQL?  (Chris Bitmead <chris.bitmead@bigfoot.com>)
List pgsql-general
>Let's say I had a table...
>CREATE TABLE book (
>   author oid,
>   name text );
>and...
>CREATE TABLE author (
>   name text );
>
>and I wanted to create a book pointing to author with name 'Tolstoy'. I
>want to do something like...
>
>INSERT INTO book(name,author) values('War and Peace',
>      (SELECT oid FROM author WHERE name = 'Tolstoy'));
>
>but this doesn't work. What is the correct syntax?
>

try \h insert in psql for a description of the correct syntax.

Your query becomes:

INSERT INTO book(author,name)
    SELECT oid,'War and Peace'
    FROM author
    WHERE name='Tolstoy';

Stuart.


+-------------------------+--------------------------------------+
| Stuart Rison            | Ludwig Institute for Cancer Research |
+-------------------------+ 91 Riding House Street               |
| Tel. (0171) 878 4041    | London, W1P 8BT, UNITED KINGDOM.     |
| Fax. (0171) 878 4040    | stuart@ludwig.ucl.ac.uk              |
+-------------------------+--------------------------------------+



pgsql-general by date:

Previous
From: Chris Bitmead
Date:
Subject: How to do this in SQL?
Next
From: "Jackson, DeJuan"
Date:
Subject: RE: [GENERAL] Performance